using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.IO;
using LispLib;

namespace GeneticAlgorithm.CP3.BitString
{
    class Crossover
    {
        public static Individual<int[], Interpreter.Pair> A(params Individual<int[], Interpreter.Pair>[] parents)
        {
            int[] parentA = parents[0].Genotype;
            int[] parentB = parents[1].Genotype;

            int aLength = parentA.Length;
            int bLength = parentB.Length;

            //reference to the shorter parent, and the longer parent
            int[] shorter;
            int[] longer;
            if (aLength < bLength)
            {
                shorter = parentA;
                longer = parentB;
            }
            else
            {
                shorter = parentB;
                longer = parentA;
            }


            //the first half of the child comes from the shorter parent
            //the second, from the longer
            int c_Length = (int)(aLength + bLength) / 2;

            int[] child = new int[c_Length];

            for (int i = 0; i < aLength; i++)
            {
                child[i] = shorter[i];
            }
            for (int i = (int)(c_Length * .5f); i < c_Length; i++)
            {
                child[i] = longer[i];
            }

            return new Individual(child, parents);


            //// offspring get half genotpe from each parent
            //int halfP1Count = parents[0].Genotype.Count / 2;
            //int halfP2Count = parents[1].Genotype.Count / 2;
            //BitArray genotype = new BitArray(halfP1Count + halfP2Count);
            //int i = 0;
            //for (; i < halfP1Count; i++)
            //{
            //    genotype.Set(i, parents[0].Genotype[i]);
            //}
            //for (; i < genotype.Count; i++)
            //{
            //    genotype.Set(i, parents[1].Genotype[halfP2Count++]);
            //}

            //Individual1 offspring = null;
            //while (offspring == null)
            //{
            //    try
            //    {
            //        offspring = new Individual1(genotype);
            //    }
            //    catch (Exception)
            //    {
            //        // genotype repair
            //        Repair(genotype);
            //    }
            //}

            //return offspring;
        }

        private static void Repair(BitArray genotype)
        {
            // TODO repair LISP
            throw new Exception("no implementation");
        }
    }
}
